package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.OrderDao;
import com.woniuxy.entity.OrderEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;
    //查找所有订单信息
    @Override
    public ResponseEntity findAllOrder() {
        return new ResponseEntity<>(200,"成功",orderDao.findAllOrder());
    }
    //根据订单号查找订单信息
    @Override
    public ResponseEntity findAllOrderByNum(String order_num) {

        return new ResponseEntity(200,"成功",orderDao.findAllOrderByNum(order_num));
    }
    //新增一条订单信息
    @Override
    public ResponseEntity addOneOrder(OrderEntity orderEntity) {
        int i = orderDao.addOneOrder(orderEntity);
        int account = orderEntity.getOrder_account();
        String material_code = orderEntity.getMaterial_code();
        BigDecimal price = (BigDecimal) orderDao.productionPrice(orderEntity.getMaterial_code());
        BigDecimal total_money = price.multiply(new BigDecimal(account));
        orderDao.addTotalMoney(orderEntity.getOrder_num(),total_money);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }
    //更新订单信息
    @Override
    public ResponseEntity updateOrder(OrderEntity orderEntity) {
        int i = orderDao.updateOrder(orderEntity);
        int account = orderEntity.getOrder_account();
        String material_code = orderEntity.getMaterial_code();
        BigDecimal price = (BigDecimal) orderDao.productionPrice(orderEntity.getMaterial_code());
        BigDecimal total_money = price.multiply(new BigDecimal(account));
        orderDao.addTotalMoney(orderEntity.getOrder_num(),total_money);

        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }
    //删除订单信息   根据订单号
    @Override
    public ResponseEntity deleteOrder(String order_num) {
        int i = orderDao.deleteOrder(order_num);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }
    //下发订单信息  根据订单号修改订单状态
    @Override
    public ResponseEntity positionOrder(String order_num) {
        int i = orderDao.positionOrder(order_num);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }
    //撤回订单   根据订单号修改状态
    @Override
    public ResponseEntity handleExit(String order_num) {
        int i = orderDao.handleExit(order_num);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }
    //终止订单   根据订单号修改订单状态
    @Override
    public ResponseEntity termination(String order_num) {
        int i = orderDao.termination(order_num);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }
    }

    //分页
    @Override
    public ResponseEntity findAllByPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<OrderEntity> list = orderDao.findAllByPage();
        PageInfo<OrderEntity> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    //订单详情
    @Override
    public ResponseEntity detailOrder(String order_num) {

        return new ResponseEntity<>(200,"成功",orderDao.findDetailOrders(order_num));
    }

    //产品编码
    @Override
    public ResponseEntity materialCodeData() {
        return new ResponseEntity<>(200,"成功",orderDao.materialCodeData());
    }

    //产品名称和类型
    @Override
    public ResponseEntity productionNameAndType(String material_code) {
        return new ResponseEntity<>(200,"成功",orderDao.productionNameAndType(material_code));
    }

    //查找产品单价
    @Override
    public ResponseEntity productionPrice(String material_code) {
        return new ResponseEntity<>(200,"成功",orderDao.productionPrice(material_code));
    }

    //查找客商
    @Override
    public ResponseEntity findMerchantsName() {
        return new ResponseEntity<>(200,"成功",orderDao.findMerchantsName());
    }

    //根据客商姓名查找客商优先级
    @Override
    public ResponseEntity getmerchants_grade(String name) {

        return new ResponseEntity<>(200,"成功",orderDao.getmerchants_grade(name));
    }
}
